package mobileann.mafamily.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.mofind.android.base.L;

import android.util.Log;
import mobileann.mafamily.act.main.FS;

public class ZLibUtils {
//	public static byte[] copressFile(String path){
//		File file = new File(path);
//		byte[] data = getBytesFromFile(file);
//		L.i("EventLog", "length: "+data.length);
//		byte[] datac = compress(data);
//		L.i("EventLog", "length: "+datac.length);
//		return compress(data);
//	}

	/**
	 * @param file
	 * @return
	 */
//	public static byte[] compressFile(File file){
//		byte[] data = getBytesFromFile(file);
//		return compress(data);
//	}

	/**
	 * 解压并转为字符串
	 * @param data
	 * @return
	 */
//	public static String deCompressByte(byte[] data){
//		return new String(decompress(data));
//	}

	/**
	 * 压缩
	 * @param data 待压缩数据
	 * @return byte[] 压缩后数据
	 */
//	public static byte[] compress(byte[] data) {
//		byte[] output = new byte[0];
//
//		Deflater compresser = new Deflater();
//		compresser.reset();
//		compresser.setInput(data);
//		compresser.finish();
//		ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
//		try {
//			byte[] buf = new byte[1024];
//			while (!compresser.finished()) {
//				int i = compresser.deflate(buf);
//				bos.write(buf, 0, i);
//			}
//			output = bos.toByteArray();
//		} catch (Exception e) {
//			output = data;
//			L.e("maf", "error--", e);
//		} finally {
//			try {
//				bos.close();
//			} catch (IOException e) {
//				L.e("maf", "error--", e);
//			}
//		}
//		compresser.end();
//		return output;
//	}

	/**
	 * 压缩 
	 * @param data 待压缩数据
	 * @param os 输出流
	 */
//	public static void compress(byte[] data, OutputStream os) {
//		DeflaterOutputStream dos = new DeflaterOutputStream(os);
//
//		try {
//			dos.write(data, 0, data.length);
//
//			dos.finish();
//
//			dos.flush();
//		} catch (IOException e) {
//			L.e("maf", "error--", e);
//		}
//	}

	/**
	 * 解压缩
	 * @param data 待解压数据
	 * @return 解压后的数据
	 */
//	public static byte[] deCompress(byte[] data) {
//		byte[] output = new byte[0];
//
//		Inflater decompresser = new Inflater();
//		decompresser.reset();
//		decompresser.setInput(data);
//
//		ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
//		try {
//			byte[] buf = new byte[1024];
//			while (!decompresser.finished()) {
//				int i = decompresser.inflate(buf);
//				o.write(buf, 0, i);
//			}
//			output = o.toByteArray();
//		} catch (Exception e) {
//			output = data;
//			L.e("maf", "error--", e);
//		} finally {
//			try {
//				o.close();
//			} catch (IOException e) {
//				L.e("maf", "error--", e);
//			}
//		}
//		decompresser.end();
//		return output;
//	} 

	/**
	 * 解压缩
	 * @param is 输入流
	 * @return 解压后的数据
	 */
//	public static byte[] decompress(InputStream is) {
//		InflaterInputStream iis = new InflaterInputStream(is);
//		ByteArrayOutputStream o = new ByteArrayOutputStream(1024);
//		try {
//			int i = 1024;
//			byte[] buf = new byte[i];
//
//			while ((i = iis.read(buf, 0, i)) > 0) {
//				o.write(buf, 0, i);
//			}
//
//		} catch (IOException e) {
//			L.e("maf", "error--", e);
//		}
//		return o.toByteArray();
//	}

	//=======================================
	/**
	 * 文件转字节数组
	 * @param path
	 * @return
	 */
	public static byte[] getBytesByPath(String path){
		File file = new File(path);
		byte[] data = getBytesFromFile(file);
		return data;
	}
	
	/**
	 * 文件转化为字节数组
	 * @param file
	 * @return
	 */
	public static byte[] getBytesFromFile(File file) {
		byte[] ret = null;
		try {
			if (file == null) {
				// log.error("helper:the file is null!");
				return null;
			}
			FileInputStream in = new FileInputStream(file);
			ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
			byte[] b = new byte[4096];
			int n;
			while ((n = in.read(b)) != -1) {
				out.write(b, 0, n);
			}
			in.close();
			out.close();
			ret = out.toByteArray();
		} catch (IOException e) {
			// log.error("helper:get bytes from file process error!");
			L.e("maf", "error--", e);
		}
		return ret;
	}

	/**
	 * 把字节数组保存为一个文件
	 * 
	 * @param b
	 * @param outputFile
	 * @return
	 */
//	public static File getFileFromBytes(byte[] b, String outputFile) {
//		File ret = null;
//		BufferedOutputStream stream = null;
//		try {
//			ret = new File(outputFile);
//			FileOutputStream fstream = new FileOutputStream(ret);
//			stream = new BufferedOutputStream(fstream);
//			stream.write(b);
//		} catch (Exception e) {
//			// log.error("helper:get file from byte process error!");
//			L.e("maf", "error--", e);
//		} finally {
//			if (stream != null) {
//				try {
//					stream.close();
//				} catch (IOException e) {
//					// log.error("helper:get file from byte process error!");
//					L.e("maf", "error--", e);
//				}
//			}
//		}
//		return ret;
//	}
	//============================================

	// 符号"/"用来作为目录标识判断符
	private static final int BUFFER = 1024;
	private static final String BASE_DIR = "";
	private static final String PATH = "/";
	
	/** 
	 * 压缩 
	 *
	 * @param srcFile 
	 * @throws Exception 
	 */
//	public static void ZipCompress(File srcFile) throws Exception {
//		String name = srcFile.getName();
//		String basePath = srcFile.getParent();
//		String destPath = basePath + File.separator + name + EXT;
//		compress1(srcFile, destPath);
//	}

	/** 
	 * 压缩 
	 *
	 * @param srcFile 
	 *源路径 
	 * @param destPath 
	 *目标路径 
	 * @throws Exception 
	 */
	public static void zipCompressByFile(File srcFile, File destFile) throws Exception {
		// 对输出文件做CRC32校验
		CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(
				destFile), new CRC32());
		ZipOutputStream zos = new ZipOutputStream(cos);
		compress3(srcFile, zos, BASE_DIR);
		zos.flush();
		zos.close();
	}

	/** 
	 * 压缩文件 
	 *
	 * @param srcFile 
	 * @param destPath 
	 * @throws Exception 
	 */
//	public static void compress1(File srcFile, String destPath) throws Exception {
//		compressByFile(srcFile, new File(destPath));
//	}

	/** 
	 * 压缩 
	 *
	 * @param srcFile 
	 *源路径 
	 * @param zos 
	 *ZipOutputStream 
	 * @param basePath 
	 *压缩包内相对路径 
	 * @throws Exception 
	 */
	private static void compress3(File srcFile, ZipOutputStream zos,
			String basePath) throws Exception {
		if (srcFile.isDirectory()) {
			compressDir(srcFile, zos, basePath);
		} else {
			compressFile(srcFile, zos, basePath);
		}
	}

	/** 
	 * 压缩 
	 *
	 * @param srcPath 
	 * @throws Exception 
	 */
//	public static void ZipCompress2(String srcPath) throws Exception {
//		File srcFile = new File(srcPath);
//		ZipCompress(srcFile);
//	}
	

	/** 
	 * 文件压缩 
	 *
	 * @param srcPath 
	 *源文件路径 
	 * @param destPath 
	 *目标文件路径 
	 *
	 */
	public static void zipCompressByPath(String srcPath, String destPath)
			throws Exception {
		File srcFile = new File(srcPath);
		File destFile = new File(destPath);
		zipCompressByFile(srcFile, destFile);
//		compress1(srcFile, destPath);
	}

	/** 
	 * 压缩目录 
	 *
	 * @param dir 
	 * @param zos 
	 * @param basePath 
	 * @throws Exception 
	 */
	private static void compressDir(File dir, ZipOutputStream zos, String basePath)
			throws Exception {
		File[] files = dir.listFiles();
		// 构建空目录
		if (files.length < 1) {
			ZipEntry entry = new ZipEntry(basePath + dir.getName() + PATH);
			zos.putNextEntry(entry);
			zos.closeEntry();
		}
		for (File file : files) {
			// 递归压缩
			compress3(file, zos, basePath + dir.getName() + PATH);
		}
	}

	/** 
	 * 文件压缩 
	 *
	 * @param file 
	 *待压缩文件 
	 * @param zos 
	 *ZipOutputStream 
	 * @param dir 
	 *压缩文件中的当前路径 
	 * @throws Exception 
	 */
	private static void compressFile(File file, ZipOutputStream zos, String dir)
			throws Exception {
		/** 
		 * 压缩包内文件名定义 
		 *
		 * <pre> 
		 * 如果有多级目录，那么这里就需要给出包含目录的文件名 
		 * 如果用WinRAR打开压缩包，中文名将显示为乱码 
		 * </pre> 
		 */
		ZipEntry entry = new ZipEntry(dir + file.getName());
		zos.putNextEntry(entry);
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
		int count;
		byte data[] = new byte[BUFFER];
		while ((count = bis.read(data, 0, BUFFER)) != -1) {
			zos.write(data, 0, count);
		}
		bis.close();
		zos.closeEntry();
	}
}
